package valuecomusa.fijibus.eticket.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import valuecomusa.fijibus.eticket.shorthaul.R;
import valuecomusa.fijibus.eticket.Utilities;
import valuecomusa.fijibus.eticket.WorkingSession;

/**
 * Created with IntelliJ IDEA.
 * User: fireman
 * Date: 6/6/13
 * Time: 12:26 AM
 * To change this template use File | Settings | File Templates.
 */
public class checkin_topup extends Activity
{
    private final String TAG = "checkin_topup";
    private double requiredFare;
    private MediaPlayer errorVoice;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checkin_topup);
        //--
        setResult(RESULT_CANCELED, null);
        //--
        Intent input = getIntent();
        requiredFare = WorkingSession.getInstance().RoundMoney(input.getDoubleExtra("REQUIRED_FARE_AMOUNT", 0));
        //--
        TextView requiredText = (TextView) findViewById(R.id.checkin_topup_required);
        requiredText.setText(WorkingSession.getInstance().decimalFormat.format(requiredFare));

        Button cancel = (Button) findViewById(R.id.checkin_topup_cancel);
        cancel.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                InputMethodManager inputManager = (InputMethodManager)
                        getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
                //--
                finish();
            }
        });

        Button submit = (Button) findViewById(R.id.checkin_topup_submit);
        submit.setOnClickListener(onSubmit);

        /*if(WorkingSession.getInstance().currentPassenger.CurrentBalance < 0)
            errorVoice = Utilities.PlayMedia(checkin_topup.this, "negative-balance.mp3");
        else
            errorVoice = Utilities.PlayMedia(checkin_topup.this, "insufficient-balance.mp3");*/
        //-
        errorVoice = Utilities.PlayMedia(checkin_topup.this, "insufficient-balance.mp3");
    }

    @Override
    protected void onStart()
    {
        super.onStart();
        Log.d(TAG, "onStart...");
    }

    private View.OnClickListener onSubmit = new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            InputMethodManager inputManager = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
            //--
            TextView errorMsg = (TextView) findViewById(R.id.checkin_topup_error_msg);
            EditText editAmount = (EditText) findViewById(R.id.checkin_topup_amount);
            if(editAmount.getText().length() == 0)
            {
                errorMsg.setText("Please enter Top up Amount");
                return;
            }
            double inputAmount = Double.parseDouble(editAmount.getText().toString());
            if(inputAmount < requiredFare)
            {
                errorMsg.setText("Invalid Top up Amount");
                return;
            }
            Intent result = new Intent();
            result.putExtra("TOP_UP_AMOUNT_RESULT", inputAmount);
            setResult(RESULT_OK, result);
            finish();
        }
    };

    @Override
    protected void onPause()
    {
        super.onPause();
        if(errorVoice != null && errorVoice.isPlaying())
        {
            errorVoice.stop();
            errorVoice = null;
        }
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        if(errorVoice != null && errorVoice.isPlaying())
        {
            errorVoice.stop();
            errorVoice = null;
        }
    }
}